package org.light.simpleauth;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.light.complexverb.ListMyActive;
import org.light.core.LayoutComb;
import org.light.core.ReportComb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.ManyToMany;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.exception.ValidateException;
import org.light.generator.Generator;
import org.light.utils.DomainUtil;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;
import org.light.verb.FindByName;

public class CorsFilterGenerator extends Generator{	
    protected Domain sampleDomain;

    public CorsFilterGenerator(Domain sampleDomain){
		super();	
		this.sampleDomain = sampleDomain;
		this.standardName = "CorsFilter";
		this.fileName = "CorsFilter.java";
	}
	
	@Override
	public StatementList generateStatementList() throws ValidateException {
    	List<Writeable> sList = new ArrayList<Writeable>();
        sList.add(new Statement(1000L,0,"package "+this.sampleDomain.getPackageToken()+".filter;"));
        sList.add(new Statement(2000L,0,""));
 
        sList.add(new Statement(3000L,0,"import java.io.IOException;"));
        sList.add(new Statement(4000L,0,""));
        sList.add(new Statement(5000L,0,"import javax.servlet.Filter;"));
        sList.add(new Statement(6000L,0,"import javax.servlet.FilterChain;"));
        sList.add(new Statement(7000L,0,"import javax.servlet.FilterConfig;"));
        sList.add(new Statement(8000L,0,"import javax.servlet.ServletException;"));
        sList.add(new Statement(9000L,0,"import javax.servlet.ServletRequest;"));
        sList.add(new Statement(10000L,0,"import javax.servlet.ServletResponse;"));
        sList.add(new Statement(10500L,0,"import javax.servlet.annotation.WebFilter;"));
        sList.add(new Statement(11000L,0,"import javax.servlet.http.HttpServletRequest;"));
        sList.add(new Statement(12000L,0,"import javax.servlet.http.HttpServletResponse;"));
        sList.add(new Statement(13000L,0,""));
        sList.add(new Statement(14000L,0,"import cn.hutool.http.Method;"));
        sList.add(new Statement(15000L,0,""));
        sList.add(new Statement(15500L,0,"@WebFilter(urlPatterns = \"/*\")"));
        sList.add(new Statement(16000L,0,"public class CorsFilter implements Filter {"));
        sList.add(new Statement(17000L,1,"@Override"));
        sList.add(new Statement(18000L,1,"public void init(FilterConfig filterConfig) throws ServletException {"));
        sList.add(new Statement(19000L,2,"// 在这里可以初始化Filter"));
        sList.add(new Statement(20000L,1,"}"));
        sList.add(new Statement(21000L,0,""));
        sList.add(new Statement(22000L,1,"@Override"));
        sList.add(new Statement(23000L,1,"public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)"));
        sList.add(new Statement(24000L,3,"throws IOException, ServletException {"));
        sList.add(new Statement(25000L,2,"HttpServletResponse res = (HttpServletResponse) response;"));
        sList.add(new Statement(26000L,0,""));
        sList.add(new Statement(27000L,2,"// 允许所有域进行跨域访问"));
        sList.add(new Statement(28000L,2,"res.setHeader(\"Access-Control-Allow-Origin\", \"*\");"));
        sList.add(new Statement(29000L,0,""));
        sList.add(new Statement(30000L,2,"// 允许的HTTP方法"));
        sList.add(new Statement(31000L,2,"res.setHeader(\"Access-Control-Allow-Methods\", \"*\");"));
        sList.add(new Statement(32000L,0,""));
        sList.add(new Statement(33000L,2,"// 允许的头部字段"));
        sList.add(new Statement(34000L,2,"res.setHeader(\"Access-Control-Allow-Headers\", \"*\");"));
        sList.add(new Statement(35000L,2,""));
        sList.add(new Statement(36000L,2,"res.setHeader(\"Access-Control-Expose-Headers\", \"*\");"));
        sList.add(new Statement(37000L,0,""));
        sList.add(new Statement(38000L,2,"// 允许带认证的请求"));
        sList.add(new Statement(39000L,2,"res.setHeader(\"Access-Control-Allow-Credentials\", \"true\");"));
        sList.add(new Statement(40000L,0,""));
        sList.add(new Statement(41000L,2,"// 预检请求的有效期，单位为秒"));
        sList.add(new Statement(42000L,2,"res.setHeader(\"Access-Control-Max-Age\", \"3600\");"));
        sList.add(new Statement(43000L,2,""));
        sList.add(new Statement(44000L,2,"if(((HttpServletRequest) request).getMethod().toUpperCase().equals(Method.OPTIONS.name())){"));
        sList.add(new Statement(45000L,3,"return;"));
        sList.add(new Statement(46000L,2,"}"));
        sList.add(new Statement(47000L,0,""));
        sList.add(new Statement(48000L,2,"chain.doFilter(request, response);"));
        sList.add(new Statement(49000L,1,"}"));
        sList.add(new Statement(50000L,0,""));
        sList.add(new Statement(51000L,1,"@Override"));
        sList.add(new Statement(52000L,1,"public void destroy() {"));
        sList.add(new Statement(53000L,2,"// 在这里可以进行Filter的资源清理"));
        sList.add(new Statement(54000L,1,"}"));
        sList.add(new Statement(55000L,0,"}"));

    	return WriteableUtil.merge(sList);
	}
}
